#include "config.h"
#include "utils.h"



Preferences prefs;

#define TAG "cfg"
int gLedState = 0;
// #define DEFAULT_MQTT_SP "broker.emqx.io"
#define DEFAULT_MQTT_URL "www.eboxmaker.com"



const char *default_https_cert =
"-----BEGIN CERTIFICATE-----\nMIID7zCCAtegAwIBAgIUQP95pWfBV2/8zNrcvVPFLx2EHAgwDQYJKoZIhvcNAQEL\
BQAwgYYxCzAJBgNVBAYTAkNOMQ4wDAYDVQQIDAVIZWJlaTERMA8GA1UEBwwITGFu\
Z2ZhbmcxDjAMBgNVBAoMBU5DSUFFMQwwCgYDVQQLDANKU0oxFTATBgNVBAMMDDEw\
LjE3Ny4yLjIwMDEfMB0GCSqGSIb3DQEJARYQc2hlbnRxbGZAMTYzLmNvbTAeFw0y\
NTAzMjQwODI0MTRaFw0yNjAzMjQwODI0MTRaMIGGMQswCQYDVQQGEwJDTjEOMAwG\
A1UECAwFSGViZWkxETAPBgNVBAcMCExhbmdmYW5nMQ4wDAYDVQQKDAVOQ0lBRTEM\
MAoGA1UECwwDSlNKMRUwEwYDVQQDDAwxMC4xNzcuMi4yMDAxHzAdBgkqhkiG9w0B\
CQEWEHNoZW50cWxmQDE2My5jb20wggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEK\
AoIBAQDNIqHVKasdi9vN6SANPSqRvrsTsTzBumzAa5RQDOMLEd5BurzeUgz/BIt+\
HSVL3+MykzcmyLw3WPICYJqbfQ7C7w0SIcvOos/pBjW0xn7saZKxPy6KQ0/93Mgj\
zilQw/cYABhVmhmnaEFwvICsq0mGMqfGW0NkzyPvByMoBH/kxzBrr3OmsPvzNtpa\
UsOMoEpokEciPfqMdm0lbURF+SF0gbcyUjhScbn7CiDjhRxSG5YAW2uxlY87tVZt\
x4iNjiTKm8kYWdK8dmMB4eVnb6//5fM2C4tuvyHgvz9PMrmMKoJk67tDrd5MbmcP\
PhzLQZpZmy6xI1QMmDBRF5BMoxM9AgMBAAGjUzBRMB0GA1UdDgQWBBQJo4GkNZd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bXBsZS5jb20wggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIB\
AQDQ8ZfnF6L/lXu2l5En4ursESnrACKqVq76UUiwqHGe5/Lv0utMGxM88hAunG2+\
S4yOcJedXoyvFLl9HqOxRxRohlk/yq41pfc3QVHK0n+xfHJ3vHsBcjjzm2bWZfF4\
flG+stpzg81tYrRkqAGTEV5Tnbw7vsbfqXRFXvasZovb8hbr57oeXGfMufITtTVZ\
braunDNU3vVQdTsbdLNk9ST8qqRjWNb5uDih1kytMRil3J1LO4C3ybceaJrNcqDj\
Drn88LkaPhkkdytGImBJ6mkIogfz/DPjquLwME+Fz7wJBdq6hyu/G2WNfU22d1Oo\
HlKnfucdd7r7GzOq71xGGBvNAgMBAAEwDQYJKoZIhvcNAQELBQADggEBAClQ6YYv\
FjLPBUSsNoMpszFypxrH+pHxgx16zsx+LlJo5l3Va8v7leYxni8IpPRBuoPHd6oU\
gVIFm7ra1bKrDv42Xx2b26bzVCjkcGVXmuY+p0sj78Mdg3YdxxlJdo76ckMah2n3\
J4NqzrKPYfRXo/ONMH2H13uUtd9V1bQpm4VeZwyZpTb3xy9/vhCaI41Jl0tcGtHI\
hlk1gIvBjHDiQtNILHD2NcDmRMY7KhiNJY4WiKpONDCNtUfUVt9ukRO0YKZeDmFy\
Y4aKk9Onkfca10zocfZRi+i+7/MKMcX77cgnlIdDpWaqbDN9w7lu54xNduMXMmu9\
f23UjROj1aLeDZk=\n-----END CERTIFICATE-----";


Config::Config()
{
}
void Config::clear()
{
    ESP_LOGD(TAG,"nvs clear!\n");
    prefs.clear();
}

void Config::begin()
{
    prefs.begin("config");
    module_name = prefs.getString(KEY_MOUDULE_NAME, "ebox_v1");
    dev_name = prefs.getString(KEY_DEV_NAME, "NULL");
    dev_pwd1 = prefs.getString(KEY_DEV_PWD1, "123");
    dev_pwd2 = prefs.getString(KEY_DEV_PWD2, "123456");
    dev_pwd3 = prefs.getString(KEY_DEV_PWD3, "12345678");
    https_cert = prefs.getString(KEY_HTTPS_CERT,"");


    
    wifi_ssid = prefs.getString(KEY_WIFI_SSID, "slab1");
    wifi_pwd = prefs.getString(KEY_WIFI_PWD, "12345678");
    wifi_mode = prefs.getUInt(KEY_WIFI_MODE, 0);
    wifi_ip.fromString(prefs.getString(KEY_WIFI_IP, "192.168.1.90"));
    wifi_subnet.fromString(prefs.getString(KEY_WIFI_SUBNET, "255.255.255.0"));
    wifi_gw.fromString(prefs.getString(KEY_WIFI_GW, "192.168.1.1"));

    dns[0].fromString(prefs.getString(KEY_DNS1, "192.168.1.1"));
    dns[1].fromString(prefs.getString(KEY_DNS2, "192.168.1.1"));

    
    mqtt_en = prefs.getUInt(KEY_MQTT_EN, 0);
    mqtt_interval = prefs.getUInt(KEY_MQTT_INTERVAL, 5);
    mqtt_url = prefs.getString(KEY_MQTT_URL, DEFAULT_MQTT_URL);
    mqtt_name = prefs.getString(KEY_MQTT_NAME, "mqtt_name");
    mqtt_pwd = prefs.getString(KEY_MQTT_PWD, "mqtt_pwd");
    mqtt_cert = prefs.getString(KEY_MQTT_PWD, default_mqtt_pem);

    ntp_server = prefs.getString(KEY_NTP_SERVER, "ntp.ntsc.ac.cn");

    if(https_cert == "")
    {
        load_default();
    }

    ESP_LOGI(TAG,"======config=========\n");
    ESP_LOGI(TAG,"module_name  :%s", module_name.c_str());
    ESP_LOGI(TAG,"dev_name     :%s", dev_name.c_str());
    ESP_LOGI(TAG,"dev_pwd1     :%s", dev_pwd1.c_str());
    ESP_LOGI(TAG,"dev_pwd2     :%s", dev_pwd2.c_str());
    ESP_LOGI(TAG,"dev_pwd3     :%s", dev_pwd3.c_str());
    ESP_LOGI(TAG,"https_cert   :%s", https_cert.c_str());


    ESP_LOGI(TAG,"ssid         :%s", wifi_ssid.c_str());
    ESP_LOGI(TAG,"pwd          :%s", wifi_pwd.c_str());
    ESP_LOGI(TAG,"wifi_mode     :%d\n", wifi_mode);
    ESP_LOGI(TAG,"wifi ip      :%s", wifi_ip.toString().c_str());
    ESP_LOGI(TAG,"wifi subnet  :%s", wifi_subnet.toString().c_str());
    ESP_LOGI(TAG,"wifi gw      :%s", wifi_gw.toString().c_str());
    ESP_LOGI(TAG,"dns1        :%s", dns[0].toString().c_str());
    ESP_LOGI(TAG,"dns2        :%s", dns[1].toString().c_str());

    ESP_LOGI(TAG,"mq_en      :%d\n", mqtt_en);
    ESP_LOGI(TAG,"mq_inr     :%d\n", mqtt_interval);
    ESP_LOGI(TAG,"mq_url     :%s", mqtt_url.c_str());
    ESP_LOGI(TAG,"mq_name    :%s", mqtt_name.c_str());
    ESP_LOGI(TAG,"mq_pwd     :%s", mqtt_pwd.c_str());
    ESP_LOGI(TAG,"mq_cert    :%s", mqtt_cert.c_str());


    ESP_LOGI(TAG,"======config end======free[%d]\n", prefs.freeEntries());

    return;
}

void Config::load_default()
{
    IPAddress ip;
    set_module_name("ebox_v1");
    set_dev_name("NULL");
    set_dev_pwd1("123");
    set_dev_pwd2("123456");
    set_dev_pwd3("12345678");
    set_https_cert(default_https_cert);


    set_wifi_ssid("slab1");
    set_wifi_pwd("12345678");
    set_wifi_mode(0);
    ip.fromString("192.168.11.90");    set_wifi_ip(ip);
    ip.fromString("255.255.255.0");    set_wifi_subnet(ip);
    ip.fromString("192.168.11.1");    set_wifi_gw(ip);

    ip.fromString("192.168.11.1");    set_dns(0,ip);
    ip.fromString("192.168.11.1");    set_dns(1,ip);



    set_mqtt_en(0);
    set_mqtt_interval(5);
    // server_port = "broker.emqx.io";
    set_mqtt_url(DEFAULT_MQTT_URL);
    set_mqtt_name("ptx4");
    set_mqtt_pwd("mqtt_pwd");
    set_mqtt_cert(default_mqtt_pem);



    set_ntp_server("ntp.ntsc.ac.cn");

    ESP_LOGD(TAG,"load_default..............ok\n");
    return;
}

void Config::set_module_name(String value)
{
    if(module_name == value) return;
    ESP_LOGD(TAG,"module_name:%s -> %s", module_name.c_str(), value.c_str());
    module_name = value;
    prefs.putString(KEY_MOUDULE_NAME, value);
    return;
}
String Config::get_module_name()
{
    return module_name;
}
void Config::set_dev_name(String value)
{
    if(dev_name == value) return;
    ESP_LOGD(TAG,"dev_name:%s -> %s", dev_name.c_str(), value.c_str());
    dev_name = value;
    prefs.putString(KEY_DEV_NAME, value);
    return;
}
String Config::get_dev_name()
{
    return dev_name;
}

void Config::set_dev_pwd1(String value)
{
    if(dev_pwd1 == value) return;
    ESP_LOGD(TAG,"dev_pwd1:%s -> %s", dev_pwd1.c_str(), value.c_str());
    dev_pwd1 = value;
    prefs.putString(KEY_DEV_PWD1, value);
    return;
}
String Config::get_dev_pwd1()
{
    return dev_pwd1;
}

void Config::set_dev_pwd2(String value)
{
    if(dev_pwd2 == value) return;
    ESP_LOGD(TAG,"dev_pwd2:%s -> %s", dev_pwd2.c_str(), value.c_str());
    dev_pwd2 = value;
    prefs.putString(KEY_DEV_PWD2, value);
    return;
}
String Config::get_dev_pwd2()
{
    return dev_pwd2;
}

void Config::set_dev_pwd3(String value)
{
    if(dev_pwd3 == value) return;
    ESP_LOGD(TAG,"dev_pwd3:%s -> %s", dev_pwd3.c_str(), value.c_str());
    dev_pwd3 = value;
    prefs.putString(KEY_DEV_PWD3, value);
    return;
}
String Config::get_dev_pwd3()
{
    return dev_pwd3;
}



void Config::set_https_cert(String value)
{
    if(https_cert == value) return;
    ESP_LOGD(TAG,"https_cert:%s -> %s", https_cert.c_str(), value.c_str());
    https_cert = value;
    prefs.putString(KEY_HTTPS_CERT, value);
    return;
}
String Config::get_https_cert()
{
    return https_cert;
}


Config config;
